<?php
/**
 * 类名称
 * User: 13651
 * Time: 2025/9/15 10:08
 */

namespace App\Repository;

use App\Models\VbillRateSaveLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class VbillRateSaveLogRepo extends CommonRepo
{

    /**
     * 新增or修改
     * @param array $data
     * @param int $id
     * @return \Illuminate\Support\HigherOrderCollectionProxy|int|mixed
     */
    public function saveData(array $data,int $id = 0)
    {
        $result = null;
        if ($id) {
            $result  = VbillRateSaveLog::query()->find($id);
        } else {
            $result = new VbillRateSaveLog();
        }
        if (empty($result))
            return 0;
        collect($data)->map(function ($val, $field) use ($result) {
            $result->{$field} = $val;
        });
        return $result->save() ? $result->id : 0;
    }

    /**
     * 根据指定条件获取数据
     * @param array $where
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getQueryData(array $where)
    {

        $query = VbillRateSaveLog::query();
        if (!empty($where['store_id']))
            $query->where('store_id', $where['store_id']);
        if (!empty($where['mno']))
            $query->where('mno', $where['mno']);
        if (!empty($where['startTime']))
            $query->whereBetween('created_at', [$where['startTime'],$where['endTime']]);
        if (isset($where['status']))
            $query->where('status', $where['status']);
        return $query->orderBy('created_at','desc')->get();
    }

    /**
     * 获取待生效数据
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getQueryDateTimeList()
    {
        /**
         * 获取昨日起始时间戳和结束时间戳
         */
        $beginYesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday = mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        $start = date('Y-m-d H:i:s',$beginYesterday);
        $end = date('Y-m-d H:i:s',$endYesterday);

        $result = VbillRateSaveLog::query()
            ->where('status',0)
            ->whereBetween('created_at',[$start,$end])
            ->orderBy('created_at')
            ->get();

        return $result;
    }
}